package com.kingyun.gpsinspection.purificationservice.services.iam.innerservice.adm.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.kingyun.gpsinspection.purificationservice.dao.mapper.iam.adm.CertificateTypeMapper;
import com.kingyun.gpsinspection.purificationservice.facade.model.BootstrapSearchBean;
import com.kingyun.gpsinspection.purificationservice.facade.model.iam.adm.CertificateType;
import com.kingyun.gpsinspection.purificationservice.facade.service.iam.adm.IamCertificateTypeService;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @version V1.0
 * @Package com.cqhilink.iot.integratedapplicationmanagement.services.innerservice.adm.impl
 * @Description: ${TODO}(证书服务实现)
 * @author: lutianbo
 * @date: 2016/12/15,0015
 */

public class IamCertificateTypeServiceImpl implements IamCertificateTypeService
{
    @Autowired
    private CertificateTypeMapper certificateTypeMapper;

    /**
     * 查询证书列表
     * @param bootstrapSearchBean  查询参数
     * @return
     */
   @Override
   public JSONObject list(BootstrapSearchBean bootstrapSearchBean)
    {
        JSONObject jsonObject = new JSONObject();
        // 分页设置
        bootstrapSearchBean.setLimit( bootstrapSearchBean.getLimit() == null ? Integer.valueOf(10) : bootstrapSearchBean.getLimit() );
        bootstrapSearchBean.setOffset( bootstrapSearchBean.getOffset() == null ? Integer.valueOf(0) : bootstrapSearchBean.getOffset() );
        // 总数
        jsonObject.put("total",certificateTypeMapper.queryCount(bootstrapSearchBean));
        List<CertificateType> trainList = certificateTypeMapper.queryList(bootstrapSearchBean);
        jsonObject.put("rows", JSON.toJSON(trainList));
        return jsonObject;
    }

    /**
     * 根据id删除证书
     * @param certificateTypeId 证书id
     * @return
     */
    @Override
    public int deleteByPrimaryKey(String[] certificateTypeId)
    {
        if(certificateTypeId.length>0){
            return certificateTypeMapper.deleteByPrimaryKey(certificateTypeId);
        }
        return 0;
    }

    /**
     * 新增证书
     * @param record 证书实体
     * @return
     */
    @Override
    public int insertSelective(CertificateType record)
    {
        if(null != record){
            return  certificateTypeMapper.insertSelective(record);
        }
        return 0;
    }

    /**
     * 根据id查询证书信息
     * @param certificateTypeId 证书id
     * @return
     */
    @Override
    public CertificateType selectByPrimaryKey(String certificateTypeId)
    {
        if(!StringUtils.isEmpty(certificateTypeId)){
            return  certificateTypeMapper.selectByPrimaryKey(certificateTypeId);
        }
        return null;
    }

    /**
     * 更新证书信息
     * @param record 证书实体
     * @return
     */
    @Override
    public int updateByPrimaryKeySelective(CertificateType record)
    {
        if(null != record){
            return  certificateTypeMapper.updateByPrimaryKeySelective(record);
        }
        return 0;
    }

    /**
     * 证书查询
     * @param name 证书名
     * @param type
     * @return
     */
    @Override
    public List<CertificateType> queryCertificateByName(String name,String  type)
    {
        Map<String,Object> map = new HashMap<>();
        if(!"".equals(name)){
            map.put("name",name);
            map.put("type",type);
            return  certificateTypeMapper.queryCertificateByName(map);
        }else{
            return  certificateTypeMapper.queryCertificateByName(map);
        }
    }
}
